package cq.zhcqk.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.webkit.JavascriptInterface;

import com.dou361.dialogui.DialogUIUtils;

import cq.zhcqk.R;
import cq.zhcqk.base.BaseActivity;
import cq.zhcqk.util.Common;
import cq.zhcqk.util.JsUtil;
import cq.zhcqk.util.WebViewHeader;

/**
 * Created by zhangle on 2018/1/24.
 */
public class ScanDetailActivity extends BaseActivity implements View.OnClickListener{

    private String titleTxt = "";
    private WebView webTest;
    private String URL = "http://106.14.202.180/blog/open/dzb/296";

    @Override
    protected void initView() {
        setContentView(R.layout.activity_scan_detail);
        webTest = (WebView) findViewById(R.id.webTest);
        setWeb();
    }

    @Override
    protected void initData() {
        String type = getIntent().getStringExtra("type");
        String val = getIntent().getStringExtra("val");
        if("scan".equals(type)){
            titleTxt = "和美磁器口";
            WebViewHeader.setHeaderAndLoad(webTest, val);
        }else if("detailUrl".equals(type)){
            titleTxt = getIntent().getStringExtra("title");
            WebViewHeader.setHeaderAndLoad(webTest,Common.URL+"/blog/open/post/"+val);
        }
        initBack();
    }

    private void initBack(){
        ImageView back = (ImageView) findViewById(R.id.header_back);
        TextView title = (TextView) findViewById(R.id.header_text);
        title.setText(titleTxt);
        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.header_back:
                if (webTest.canGoBack())
                {
                    webTest.goBack(); //goBack()表示返回WebView的上一页面
                }else
                {
                    finish();
                }
                break;
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
    }

    @SuppressLint({"JavascriptInterface"})
    public void setWeb(){
        WebSettings settings = webTest.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setBuiltInZoomControls(true);
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);
        settings.setDatabaseEnabled(true);
        settings.setSupportZoom(true);
//        settings.setDisplayZoomControls(true);
        webTest.addJavascriptInterface(new JavascriptInterface(this), "imagelistner");
        if(webTest != null)
        {
            webTest.setWebViewClient(new WebViewClient()
            {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {

                    return super.shouldOverrideUrlLoading(view, url);
                }
                @Override
                public void onPageFinished(WebView view,String url)
                {
                    super.onPageFinished(view, url);
                    DialogUIUtils.dismiss(buildBean);
                    JsUtil.addImageClickListner(webTest);
                }

                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                    buildBean.show();
                }
            });
        }
    }

//    // 注入js函数监听
//    private void addImageClickListner() {
//        // 这段js函数的功能就是，遍历所有的img几点，并添加onclick函数，
//        //函数的功能是在图片点击的时候调用本地java接口并传递url过去
//        webTest.loadUrl("javascript:(function(){" +
//                "var objs = document.getElementsByTagName(\"img\"); " +
//                "for(var i=0;i<objs.length;i++)  " +
//                "{"
//                + "    objs[i].onclick=function()  " +
//                "    {  "
//                + "        window.imagelistner.openImage(this.src);  " +
//                "    }  " +
//                "}" +
//                "})()");
//    }
//
//    // js通信接口
//
    public class JavascriptInterface {

        private Context context;

        public JavascriptInterface(Context context) {
            this.context = context;
        }
        @android.webkit.JavascriptInterface
        public void openImage(String img) {
            System.out.println(img);

            Intent intent = new Intent();
            intent.putExtra("image", img);
            intent.setClass(context, ShowWebImageActivity.class);
            context.startActivity(intent);
            System.out.println(img);
        }
    }

}
